<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/19
 * Time: 11:38
 */

namespace common\sources\read\taskorder;

use Yii;
use common\models\craftsman\TaskOrder;

class TaskOrderAccess implements ITaskOrderAccess
{
    public function getTaskOrderByNum($shop_order_num)
    {
        return TaskOrder::find()
            ->where("pay_order_num = '".$shop_order_num."'")
            ->count();

    }
    public function getTaskOrderCount($data)
    {
        $where = $this->getWhere($data);
        return TaskOrder::find()
                ->where($where)
                ->count();
    }

    public function getTaskOrderAllCount($data)
    {
        return TaskOrder::find()
            ->where(" buy_id = " .$data['buy_id'])
            ->count();
    }

    public function getTaskOrderAll($data)
    {
        $where = $this->getWhere($data);
        return TaskOrder::find()
                ->select("id,plat_type,task_type,task_commod_id,buy_id,tbname,main_url1,main_url2,title,price,num,size,ship_addr,shop_name,port_type,task_id,order_status,actual_price,advance_amount,advance_amount,refund_amount,commission,receive_name,receive_phone,
                pro_name,pro_code,city_name,city_code,area_name,area_code,addr,return_type,task_order_num,create_time,task_shop_status,parise_url,tao_parise_url,keyword_url")
                ->where($where)
                ->offset(($data['page_num']-1)*$data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
    }

    private function getWhere($data)
    {
        $sql = " buy_id = " .$data['buy_id'];
        if(!empty($data['tbname']) && isset($data['tbname']))
        {
            $sql .= " AND tbname = '".$data['tbname']."'";
        }
        if(!empty($data['plat_type']) && isset($data['plat_type']))
        {
            $sql .= " AND plat_type = ".$data['plat_type'];
        }
        if(!empty($data['task_type']) && isset($data['task_type']))
        {
            $sql .= " AND task_type = ".$data['task_type'];
        }
        if(!empty($data['order_status']) && isset($data['order_status']))
        {
            $sql .= " AND order_status = ".$data['order_status'];
        }
        return $sql;
    }

   public function getTaskOrderLists($task_commod_id)
   {
       return TaskOrder::find()
                ->select("buy_id,tbname")
                ->where("task_commod_id =". $task_commod_id." AND order_status in(1,2,3,4,5,6,7,8,9)")
                ->asArray()
                ->all();
   }

   public function getTaskOrderDetail($task_order_num)
   {
       return TaskOrder::find()
                ->select("id,plat_type,task_type,task_commod_id,buy_id,tbname,main_url1,main_url2,title,price,num,size,ship_addr,shop_name,shop_id,port_type,task_id,create_time,order_status,advance_amount,refund_amount,commission,receive_name,receive_phone,
                pro_name,pro_code,city_name,city_code,area_name,area_code,addr,return_type,task_order_num,browse_time,order_time,merchant_time,favorable_time,plat_return_time,confirm_time,reject_time,give_up_time,range_price,bus_pay_url,serial_num,
                ww_name,is_check,task_shop_status,colour,addr_url,pay_url,parise_url,tao_parise_url,keyword_url,pay_order_num,qr_code_url,keywords,task_shop_image_id,praise_keywords,search_time,actual_price,task_commod_keyword_id,praise_keywords_id")
                ->where(["task_order_num" => $task_order_num])
                ->asArray()
                ->one();
   }
   public function getTaskOrderById($task_ordr_id)
   {
       return TaskOrder::find()
           ->select("id,plat_type,task_type,task_commod_id,bus_id,buy_id,tbname,main_url1,main_url2,title,price,num,size,ship_addr,shop_name,shop_id,port_type,task_id,order_status,advance_amount,refund_amount,commission,receive_name,receive_phone,
                pro_name,pro_code,city_name,city_code,area_name,area_code,addr,return_type,task_order_num,browse_time,order_time,merchant_time,favorable_time,plat_return_time,confirm_time,reject_time,give_up_time,search_time,bus_pay_url,serial_num,
                ww_name,range_price,is_check,task_shop_status,addr_url,pay_url,parise_url,tao_parise_url,keyword_url,pay_order_num,qr_code_url,keywords,task_shop_image_id,task_commod_keyword_id,praise_keywords_id")
           ->where(["id" => $task_ordr_id])
           ->asArray()
           ->one();
   }

   public function getSuccessTaskOrderCount($data)
   {
       return TaskOrder::find()
                ->where(['buy_id' => $data['buy_id'],'order_status' => 8])
                ->count();
   }

   public function getSuccessTaskOrderList($data)
   {
       return  TaskOrder::find()
                ->select("id,plat_type,commission,task_order_num,tbname,refund_amount,return_type")
                ->where(['buy_id' => $data['buy_id'],'order_status' => 8])
                ->orderBy("create_time")
                ->offset(($data['page_num'] -1) * $data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
   }

   public function getTaskOrderByIds($task_order_ids)
   {
       return  TaskOrder::find()
           ->select("id,plat_type,,shop_name,tbname,colour,size,main_url1,title,advance_amount,task_order_num,create_time")
           ->where("id in (".$task_order_ids.")")
           ->orderBy("create_time")
           ->asArray()
           ->all();
   }
   public function getTaskOrderStatus($buy_id, $task_status)
   {
       return TaskOrder::find()
           ->select("id,plat_type,task_type,buy_id,tbname,main_url1,title,price,num,size,shop_name,task_id,order_status,advance_amount,refund_amount,actual_price,commission,return_type,task_order_num,
           create_time,task_shop_status,task_order_num,apply_success_time,confirm_time")
           ->where(" buy_id = ".$buy_id." AND return_type = 1 AND order_status in (".$task_status.")")
           ->orderBy("create_time desc")
           ->asArray()
           ->all();
   }
   public function getTaskOrderStatusCount($buy_id, $task_status)
   {
       return TaskOrder::find()
                ->where(" buy_id = ".$buy_id." AND return_type = 1 AND order_status in (".$task_status.")")
                ->count();
   }

   public function getTaskOrderCompleteCount($task_commod_id)
   {
        return TaskOrder::find()
                ->where("task_commod_id = ".$task_commod_id." AND order_status = 8")
                ->count();
   }

   public function getTaskOrderRefundSum($buy_id, $task_status)
   {
       return TaskOrder::find()
           ->select("sum(actual_price) as total_refund_amount")
           ->where(" buy_id = ".$buy_id." AND return_type = 1 AND order_status in (".$task_status.")")
           ->asArray()
           ->one();
   }

   public function getTaskOrderByStatusIds($task_order_ids)
   {
       return TaskOrder::find()
           ->select("id,plat_type,task_type,buy_id,tbname,main_url1,title,price,num,size,shop_name,task_id,order_status,refund_amount,return_type,task_order_num,create_time,task_shop_status,task_order_num")
           ->where(" id in (".$task_order_ids.")")
           ->orderBy("create_time desc")
           ->asArray()
           ->all();
   }

   public function getBuyTaskOrderDayCount($buy_id,$status,$start_time,$end_time)
   {
       $sql = " buy_id = ".$buy_id." AND order_status = ".$status ." AND create_time >=".$start_time." AND create_time <=".$end_time;
       return TaskOrder::find()
           ->where($sql)
           ->count();
   }

   public function getBuyTaskOrderCount($buy_id, $status)
   {
       $sql = " buy_id = ".$buy_id." AND order_status = ".$status ;
       return TaskOrder::find()
           ->where($sql)
           ->count();
   }
   //判断该任务是否已经接过
    public function getTaskOrderByTaskCommodId($buy_id,$task_commod_id)
    {
        $sql = " buy_id = ".$buy_id." AND task_commod_id = ".$task_commod_id ;
        return TaskOrder::find()
            ->where($sql)
            ->count();
    }
    //一个买号一个时间端内接任务数量
    public function getTaskTidCount($tb_id,$status,$start_time,$end_time)
    {
        $sql = " tb_id = ".$tb_id." AND order_status in (".$status .")  AND create_time >=".$start_time." AND create_time <=".$end_time;
        return TaskOrder::find()
            ->where($sql)
            ->count();
    }
    //一个买号一个店铺一个月内不能再接
    public function getTaskOrderByShopId($tb_id,$start_time,$end_time,$order_statuses)
    {
        $sql = " tb_id = ".$tb_id." AND order_status in (".$order_statuses.")  AND create_time >=".$start_time." AND create_time <=".$end_time;
        return TaskOrder::find()
                ->select("shop_id")
                ->where($sql)
                ->groupBy("shop_id")
                ->asArray()
                ->all();
    }
    //按照天统计总人数
    public function getTaskOrderByBuyIdCount($data)
    {
        $where = " buy_id in (".$data['buy_ids'].")  AND order_status in (".$data['order_status'].")";
        $sql = "select  from_unixtime(create_time, '%Y-%m-%d') AS `days`,count(buy_id) as buy_count,count(id) as task_ount from  task_order where ".$where." GROUP BY `days` ORDER BY `days` DESC";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    //按天统计接单人数和接单数量
    public function getTaskOrderByBuyID($data)
    {
        $where = " buy_id in (".$data['buy_ids'].")  AND order_status in (".$data['order_status'].")";
        $sql = "select  from_unixtime(create_time, '%Y-%m-%d') AS `days`,count(buy_id) as buy_count,count(id) as task_ount from  task_order where ".$where." GROUP BY `days` ORDER BY `days` DESC";
        return Yii::$app->db->createCommand($sql)->queryAll();

    }

    //获取任务排名奖励
    public function getTaskOrderSort($data)
    {
        $sql = "select sum(b.income) as income,b.task_order_id as task_order_id,t.buy_id as buy_id
                from buy_pepper_record b
                LEFT JOIN buyuser  u on b.buy_id = u.id
                LEFT JOIN task_order t on b.task_order_id = t.id
                where from_unixtime(b.create_time,'%Y-%m-%d') = '".$data['days']."' and b.type = 7 AND b.buy_id = ".$data['buy_id']."
                GROUP BY t.buy_id
                order by b.income desc;";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }
    //任务单数量展示
    public function getTaskOrderPepper($data)
    {
        $sql = "select t.actual_refunds as actual_refunds,t.task_order_num as task_order_num, b.income as income
                from task_order t
                LEFT JOIN buy_pepper_record  b
                on t.id = b.task_order_id 
                where b.buy_id = ".$data['buy_id']." and b.type =7
                ;";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }
    //买手垫付本金
    public function getBuyAdvanceFund($data)
    {
        return TaskOrder::find()
                ->select("sum(advance_amount) as advance_amount")
                ->where(" buy_id =".$data['buy_id']."  AND order_status in (".$data['order_status'].")")
                ->asArray()
                ->one();
    }

}